package com.cf.base.comm.widget.watermark;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;

import com.cf.base.comm.R;
import com.cf.base.comm.tools.DisplayUtils;
import com.cf.base.comm.tools.ThemeUtils;


/**
 * 水印控件
 *
 * @author yangji
 */
@SuppressLint("ViewConstructor")
public class WatermarkView extends AppCompatImageView {

    private int mWidthPixels = 0;
    private int mHeightPixels = 0;
    private int textSize = 20;
    private String waterMarkText;
    private Paint paint;
    private int textColor = 0x80d1d1d1;

    public WatermarkView(Context context) {
        super(context);
        init();
    }

    public void setWatermarkText(CharSequence text) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 15; i++) {
            sb.append(text).append(" ");
        }
        waterMarkText = sb.toString();
        postInvalidate();
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        postInvalidate();
    }

    public void setTextColor(int textColor) {
        paint.setColor(textColor);
        postInvalidate();
    }

    private void init() {
        setId(R.id.comm_lib_watermark_view_id);
        textSize = DisplayUtils.singleton.get().sp2px(textSize);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setAlpha(70);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        resetWh();
        postInvalidate();
    }


    @Override
    @SuppressLint("DrawAllocation")
    protected void onDraw(Canvas canvas) {
        if (waterMarkText == null) {
            return;
        }

        if (mWidthPixels == 0) {
            return;
        }

        clipRect(canvas);
        int size = Math.max(mHeightPixels, mWidthPixels);

        canvas.rotate(-45, 0, size);
        int lineHeight = size / 7;
        int temp = lineHeight;

        while (temp < 2 * size) {
            canvas.drawText(waterMarkText, 0, temp, paint);
            temp += lineHeight;
        }
    }

    private int[] getViewLocation(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        return location;
    }


    private void clipRect(Canvas canvas) {

        if (getContext() instanceof Activity) {
            Activity activity = (Activity) getContext();

            Rect rect = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

            View topView = activity.findViewById(R.id.comm_lib_sun_tool_bar_group);
            View bottomView = activity.findViewById(R.id.comm_lib_sun_bottom_bar_group);

            int left = 0;
            int top = 0;
            int right = mWidthPixels;
            int bottom = mHeightPixels;

            if (topView != null) {
                // int[] location = getViewLocation(view);
                top = Math.max(Math.max(topView.getHeight(), topView.getMeasuredHeight()), topView.getLayoutParams().height);
            }
            if (bottomView != null) {
                int[] location = getViewLocation(bottomView);
                bottom = location[1] - top;
            }
            canvas.clipRect(left, top, right, bottom);

        } else {
            int[] location = getViewLocation(this);
            int x = location[0];
            int y = location[1];
            canvas.translate(x, y);
        }
    }

    private void resetWh() {
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        mWidthPixels = dm.widthPixels;
        mHeightPixels = dm.heightPixels;
    }


}
